#include "DefaultGUIActionsManager.h"


DefaultGUIActionsManager::DefaultGUIActionsManager(MainComponent * mainComponent) : GUIActionsManager(mainComponent),
																					isElementDragging (false)
{
	
}

DefaultGUIActionsManager::~DefaultGUIActionsManager()
{
}

void DefaultGUIActionsManager::mouseEnterApp(const MouseEvent& e)
{
	animator->mouseEnterApp(e);
}

void DefaultGUIActionsManager::mouseEnterElement(ElementComponent * element, const MouseEvent& e)
{
	if(element->getDockElement()->getType() != separator)
	{
		mainComponent->showTitle(element);
		//animator->mouseEnterElement(element, e);
	}

	mouseEnterApp(e);
}

void DefaultGUIActionsManager::mouseExitApp(const MouseEvent& e)
{
	animator->mouseExitApp(e);
}

void DefaultGUIActionsManager::mouseExitElement(ElementComponent * element, const MouseEvent& e)
{
	mainComponent->hideTitle();
	//animator->mouseExitElement(element, e);

	mouseExitApp(e);
}

void DefaultGUIActionsManager::mouseDownOnApp(const MouseEvent& e)
{
	if(e.mods.isRightButtonDown())
	{
		PopupMenu m;
        m.addItem (1, T("Close the Dock"));

        int result = m.showAt (0);

        if (result != 0)
        {
			switch(result)
			{
				case 1:
					JUCEApplication::quit();
					break;
			}
		}
	}
	else
	{
		CircularMenu menu;
			
		Path p;
		p.addArrow(0,0,50,20,2,10,20);
		DrawablePath * edit = new DrawablePath();
		edit->setPath(p);
		DrawablePath * cc = new DrawablePath();
		p.clear();
		p.addBubble(10,10,40,50,4,3,5,0,4,10);
		cc->setPath(p);
		menu.addItem(1, edit, "Copy box");
		menu.addItem(2, cc, "Cut box");
		menu.show(56 /*radius*/); 
	}

	animator->mouseDownOnApp(e);
}

void DefaultGUIActionsManager::mouseDownOnElement(ElementComponent * element, const MouseEvent& e)
{
	element->getImageHolder()->setMouseDown(true);
	animator->mouseDownOnElement(element, e);

	//mouseDownOnApp(e);
}

void DefaultGUIActionsManager::mouseUp(Component * comp, const MouseEvent& e)
{
	if(dynamic_cast<ElementComponent*>(comp) != 0)
	{
		ElementComponent * obj = dynamic_cast<ElementComponent*>(comp);

		obj->getImageHolder()->setMouseDown(false);
		
		if(e.mods.isLeftButtonDown() && e.mouseWasClicked())
		{
			obj->getDockElement()->leftMouseButtonClick();
			//mainComponent->removeUselessChildContainers(obj);
		}
		
		if(isElementDragging)
		{
			animator->endDraggingElement(obj, e);
			ElementComponent * target = dynamic_cast<ElementComponent*>(Desktop::getInstance().findComponentAt(e.getScreenX(), e.getScreenY()));
			if(target == 0)
			{
				mainComponent->removeElement(obj);	
			}
			else
			{
				mainComponent->moveElement(obj, target);
			}
		}
	}
	else
	{
	}
	
	animator->mouseUp(comp, e);
}

void DefaultGUIActionsManager::mouseDragElement(ElementComponent * element, const MouseEvent& e)
{
	isElementDragging = true;
	DragAndDropContainer * container = DragAndDropContainer::findParentDragContainerFor(element);
	container->startDragging("Element", element, 0, true);

	animator->mouseDragElement(element, e);
}
